<?php

namespace app\admin\controller;

class Map extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '地图管理',
                'description' => '订单管理',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('info'),
                ),
            ),
        );
    }

    public function index()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $username = $request->post('username');

        if ($id) {
            $where['qyid'] = $id;
        }
        if ($username) {
            $where['qyname'] = $username;
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['username'] = $username;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('Map')->loadList($where, $pageAry);
        if (!empty($list)) {
            $mdoelArea = model('MapArea');
            foreach ($list as $key => $val) {
                $map['mid'] = $val['mid'];
                $val['areaName'] = $mdoelArea->findValue($map, 'mname');

                $numArea['mqy'] = $val['qyid'];
                $val['countArea'] = $mdoelArea->countNum($numArea);
            }
        }
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        return $this->fetch('index');
    }

    public function info()
    {
        $request = request();
        $id = input('qyid');
        $model = model('Map');
        if ($request->method() == 'POST') {
            if ($id) {
                $status = $model->editAll($request);
            } else {
                $status = $model->addall($request);
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url' => url('index')]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            $info = $model->getInfo($id);
            $this->assign('info', $info);

            $sub['mqy'] = $id;
            $list = model('MapArea')->getList($sub);
            $this->assign('list', $list);

            $this->fetch('info');
        }
    }

    public function area()
    {
        //筛选条件
        $where = [];
        $pageMaps = [];

        $mqy = input('mqy');
        $mname = input('mname');

        if ($mqy) {
            $where['mqy'] = $mqy;
        }
        if ($mname) {
            $where['mname'] = $mname;
        }

        //URL参数
        $pageMaps['mqy'] = $mqy;
        $pageMaps['mname'] = $mname;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'area',
            'query' => $pageMaps,
        ];

        //查询数据
        $model = model('MapArea');
        $list = $model->loadList($where, $pageAry);
        if (!empty($list)) {
            $mdoelMap = model('Map');
            foreach ($list as $key => $val) {
                //怪物
                $gwAry = ($val['mgid'] != '' && $val['mgid'] != null) ? explode(',', $val['mgid']) : [];
                $gwList = [];
                if (!empty($gwAry)) {
                    foreach ($gwAry as $k => $item) {
                        $gwAryInfo = explode('|', $item);
                        $infoMonster = model('Monster')->getInfo($gwAryInfo[0]);

                        $gwList[$k]['id'] = $gwAryInfo[0];
                        $gwList[$k]['num'] = $gwAryInfo[1];
                        $gwList[$k]['lv'] = $infoMonster['glv'];
                        $gwList[$k]['name'] = $infoMonster['gname'];
                    }
                }
                $val['gw_list'] = $gwList;

                //npc
                $npcAry = ($val['mnpc'] != '' && $val['mnpc'] != null) ? explode(',', $val['mnpc']) : [];
                $npcList = [];
                if (!empty($npcAry)) {
                    foreach ($npcAry as $k => $item) {
                        $infoNpc = model('Npc')->getInfo($item);
                        $npcList[$k]['id'] = $infoNpc['id'];
                        $npcList[$k]['name'] = $infoNpc['nname'];
                        $npcList[$k]['lv'] = $infoNpc['nlv'];
                    }
                }
                $val['npc_list'] = $npcList;

                //boss
                $val['bossinfo'] = '';
                if ($val['midboss'] != 0) {
                    $bossinfo = model('Boss')->getInfo($val['midboss']);
                    if (!empty($bossinfo)) {
                        $val['bossinfo'] = '[lv:' . $bossinfo['bosslv'] . ']' . $bossinfo['bossname'];
                    }
                }

                //归属
                $val['qyname'] = $mdoelMap->findValue(['qyid' => $val['mqy']], 'qyname');

                //区域
                $val['mup_name'] = '';
                if ($val['mup'] != 0) {
                    $val['mup_name'] = $model->findValue(['mid' => $val['mup']], 'mname');
                }
                $val['mleft_name'] = '';
                if ($val['mleft'] != 0) {
                    $val['mleft_name'] = $model->findValue(['mid' => $val['mleft']], 'mname');
                }
                $val['mright_name'] = '';
                if ($val['mright'] != 0) {
                    $val['mright_name'] = $model->findValue(['mid' => $val['mright']], 'mname');
                }
                $val['mdown_name'] = '';
                if ($val['mdown'] != 0) {
                    $val['mdown_name'] = $model->findValue(['mid' => $val['mdown']], 'mname');
                }
            }
        }
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        return $this->fetch('area');
    }

    public function edit()
    {
        $request = request();
        $id = input('mid');
        $model = model('MapArea');
        $info = $model->getInfo($id);

        if ($request->method() == 'POST') {
            $mgid = '';
            if (!empty($_POST['mgid'])) {
                //预处理操作方法数据
                $mgid = $this->getAryGw($_POST['mgid']);
            }
            $_POST['mgid'] = $mgid;

            $mnpc = '';
            if (!empty($_POST['mnpc'])) {
                //预处理操作方法数据
                $mnpc = $this->getAryNpc($_POST['mnpc']);
            }
            $_POST['mnpc'] = $mnpc;

            if ($id) {
                $status = $model->editAll($request);
                $mqy = $info['mqy'];
            } else {
                $status = $model->addall($request);
                $mqy = $_POST['mqy'];
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url' => url('area', ['mqy' => $mqy])]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            if (!empty($info)) {
                $info['mup_name'] = '没有道路';
                if ($info['mup'] != 0) {
                    $info['mup_name'] = $model->findValue(['mid' => $info['mup']], 'mname');
                }
                $info['mleft_name'] = '没有道路';
                if ($info['mleft'] != 0) {
                    $info['mleft_name'] = $model->findValue(['mid' => $info['mleft']], 'mname');
                }
                $info['mright_name'] = '没有道路';
                if ($info['mright'] != 0) {
                    $info['mright_name'] = $model->findValue(['mid' => $info['mright']], 'mname');
                }
                $info['mdown_name'] = '没有道路';
                if ($info['mdown'] != 0) {
                    $info['mdown_name'] = $model->findValue(['mid' => $info['mdown']], 'mname');
                }

                $info['mgid_list'] = [];
                if ($info['mgid'] != '') {
                    $ary = [];
                    $mgidAry = explode(',', $info['mgid']);
                    foreach ($mgidAry as $k => $item) {
                        $mgidAryInfo = explode('|', $item);
                        $infoMonster = model('Monster')->getInfo($mgidAryInfo[0]);

                        $ary[$k]['id'] = $mgidAryInfo[0];
                        $ary[$k]['num'] = $mgidAryInfo[1];
                        $ary[$k]['name'] = '[lv:' . $infoMonster['glv'] . ']-' . $infoMonster['gname'];
                    }
                    $info['mgid_list'] = $ary;
                }

                $info['mnpc_list'] = [];
                if ($info['mnpc'] != '') {
                    $ary = [];
                    $mnpcAry = explode(',', $info['mnpc']);
                    foreach ($mnpcAry as $k => $item) {
                        $infoNpc = model('Npc')->getInfo($item);

                        $ary[$k]['id'] = $item;
                        $ary[$k]['name'] = '[' . $infoNpc['nsex'] . ']-' . $infoNpc['nname'];
                    }
                    $info['mnpc_list'] = $ary;
                }
            }
            $this->assign('info', $info);
            //echo "<pre>";print_r($info);die;

            $num_mgid = empty($info) ? 1 : count($info['mgid_list']) + 1;
            $this->assign('num_mgid', $num_mgid);
            $mnpc_list = empty($info) ? 1 : count($info['mnpc_list']) + 1;
            $this->assign('num_mnpc', $mnpc_list);

            $list = model('Map')->getList();
            $this->assign('list', $list);

            $listBoss = model('Boss')->getList();
            $this->assign('listBoss', $listBoss);

            $this->fetch('edit');
        }
    }

    protected function getAryGw($data)
    {
        $ary = [];
        if (!empty($data)) {
            foreach ($data as $key => $vo) {
                $ary[$key] = $vo['id'] . '|' . $vo['num'];
            }
        }
        return implode(',', $ary);
    }

    protected function getAryNpc($data)
    {
        $ary = [];
        if (!empty($data)) {
            foreach ($data as $key => $vo) {
                $ary[$key] = $vo['id'];
            }
        }
        return implode(',', $ary);
    }

    public function traffic()
    {
        $id = input('id', 0);
        $list = model('MapArea')->getList();
        if (!empty($list)) {
            foreach ($list as $item) {
                $item['qyname'] = model('Map')->findValue(['qyid' => $item['mqy']], 'qyname');
            }
        }
        $this->assign('list', $list);
        $this->assign('id', $id);

        return $this->fetch('traffic');
    }

    public function getGwList()
    {
        $list = model('Monster')->getList();
        $this->assign('list', $list);
        return $this->fetch('monster');
    }

    public function getNpcList()
    {
        $list = model('Npc')->getList([],'nlv asc,id desc');
        $this->assign('list', $list);
        return $this->fetch('people');
    }


    /**
     * 删除
     */
    public function del()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        if (model('Map')->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }

    public function delArea()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        if (model('MapArea')->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
}

